package com.example.imtoken.modules.main.wallet.details.trade.record

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BusUtils
import com.example.imtoken.R
import com.example.imtoken.core.base.BaseFragment
import com.example.imtoken.core.utils.BusConfig
import com.example.imtoken.databinding.TransactionRecordFragmentBinding
import com.example.imtoken.modules.main.wallet.details.trade.details.TransferDetailsActivity

class TransactionRecordFragment :
    BaseFragment<TransactionRecordViewModel, TransactionRecordFragmentBinding>(
        TransactionRecordViewModel::class.java
    ) {

    private val mViewModel by lazy {
        getViewModelThis(TransactionRecordViewModel::class.java)
    }

    private var type = -1
    private var coin: String? = null

    private lateinit var adapter: TransactionRecordAdapter

    override fun onResume() {
        super.onResume()
        if (!mViewModel.isLoadData) {
            mViewModel.type = type
            mViewModel.coin = coin
            mViewModel.getTransactions()
            mViewModel.isLoadData = true
        }
    }

    override fun initLayoutId(): Int {
        return R.layout.transaction_record_fragment
    }

    fun setType(type: Int): TransactionRecordFragment {
        this.type = type
        return this
    }

    fun setCoin(coin: String?): TransactionRecordFragment {
        this.coin = coin
        return this
    }

    override fun init() {
        BusUtils.register(this)
        initAdapter()
        initObserve()
    }

    @BusUtils.Bus(tag = BusConfig.update_data_sticky_wallet_details_activity, sticky = true)
    fun updateData() {
        mViewModel.getTransactions()
    }

    private fun initObserve() {
        mViewModel.transactionLiveData.observe(this) {
            try {
                val data = it.getOrNull()
                if (data != null) {
                    mViewModel.transaction.addAll(data)
                    adapter.setList(mViewModel.transaction)
                    if (data.size == 50) {
                        adapter.loadMoreModule.loadMoreComplete()
                    } else {
                        adapter.loadMoreModule.loadMoreEnd(true)
                    }
                } else {
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            } catch (e: Exception) {
                adapter.loadMoreModule.loadMoreFail()
                e.printStackTrace()
            }
        }
    }

    private fun initAdapter() {
        adapter = TransactionRecordAdapter(R.layout.item_transaction_record1)
        val layoutManager = object : LinearLayoutManager(activity) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        dataBinding.rvRecord.layoutManager = layoutManager
        dataBinding.rvRecord.adapter = adapter
        adapter.setEmptyView(R.layout.empty_view)
        adapter.setOnItemClickListener { _, _, position ->
            TransferDetailsActivity.gotoActivity(context, adapter.data[position])
        }
        adapter.loadMoreModule.isAutoLoadMore = true
        adapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
        adapter.loadMoreModule.setOnLoadMoreListener {
            mViewModel.getMoreTransactions()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        BusUtils.removeSticky(BusConfig.update_data_sticky_wallet_details_activity)
        BusUtils.unregister(this)
    }

}